import { getToken } from "@/utils/common";
import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      name: "index",
      redirect:'/home',
      component: () => import("@/layout/index.vue"),
      children: [
        {
          path: "/home",
          name: "home",
          meta: {
            title: "首页",
            hideNavbar: true,
            hideAsideMenu: true,
          },
          component: () => import("@/pages/examples/home/index.vue"),
        },
        {
          path: "/follow/obtainFollowUpStatus",
          name: "obtainFollowUpStatu",
          meta: {
            title: "获取随访状态",
          },
          component: () => import("@/pages/examples/obtainFollow/index.vue"),
        },
        {
          path: "/follow/toBeFollowedUpp",
          name: "toBeFollowedUp",
          meta: {
            title: "待随访记录",
          },
          component: () => import("@/pages/examples/toBeFollowedUp/index.vue"),
        },

        {
          path: "/library",
          name: "library",
          meta: {
            title: "居民库",
            hideNavbar: true,
            hideAsideMenu: true,
          },
          component: () => import("@/pages/examples/library/index.vue"),
        },
        {
          path: "/archive",
          name: "archive",
          meta: {
            title: "档案管理",
            hideNavbar: true,
            hideAsideMenu: true,
          },
          component: () => import("@/pages/examples/archive/index.vue"),
        },
        
        {
          path: "/physicals/add",
          name: "healthadd",
          meta: {
            title: "体检记录",
          },
          component: () => import("@/pages/examples/pe/index.vue"),
        },
        {
          path: "/physicals/admin",
          name: "healthyadmin",
          meta: {
            title: "体质辨识记录",
          },
          component: () => import("@/pages/examples/healthy/index.vue"),
        },
        {
          path: "/physicals/addHealth",
          name: "examplesaddHealth",
          meta: {
            title: "新增体质辨识",
          },
          component: () => import("@/pages/examples/addHealth/index.vue"),
        },
        {
          path: "physicals/health",
          name: "exampleshealth",
          meta: {
            title: "新增体检",
          },
          component: () => import("@/pages/examples/health/index.vue"),
        },

        {
          path: "/physicals/bloodAnalysis",
          name: "examplesbloodAnalysis",
          meta: {
            title: "血液检验",
          },
          component: () => import("@/pages/examples/bloodAnalysis/index.vue"),
        },
        {
          path: "/physicals/urine",
          name: "examplesurine",
          meta: {
            title: "尿液检验",
          },
          component: () => import("@/pages/examples/urine/index.vue"),
        },
        {
          path: "/physicals/heart",
          name: "examplesheart",
          meta: {
            title: "心电图",
          },
          component: () => import("@/pages/examples/heart/index.vue"),
        },
        {
          path: "/physicals/abdomen",
          name: "examplesabdomen",
          meta: {
            title: "腹部B超",
          },
          component: () => import("@/pages/examples/abdomen/index.vue"),
        },
        {
          path: "/settings/drugs",
          name: "examplesdrugs",
          meta: {
            title: "药品目录",
          },
          component: () => import("@/pages/examples/drugs/index.vue"),
        },
        {

          path: "/settings/rpa",
          name: "examplesrpa",
          meta: {
            title: "流程管理",
          },
          component: () => import("@/pages/examples/rpa/index.vue"),
        },
        {
          path: "/settings/task",
          name: "examplesTask",
          meta: {
            title: "任务调度",
          },
          component: () => import("@/pages/examples/task/index.vue"),
        },
        {
          path: "/settings/operation",
          name: "examplesOperation",
          meta: {
            title: "操作日志",
          },
          component: () => import("@/pages/examples/operation/index.vue"),
        },
        {
          path: "/settings/users",
          name: "examplesUsers",
          meta: {
            title: "系统账号设置",
          },
          component: () => import("@/pages/examples/users/index.vue"),
        },
        {
          path: "/settings/his",
          name: "examplesHis",
          meta: {
            title: "HIS账号设置",
          },
          component: () => import("@/pages/examples/his/index.vue"),
        },
        {
          path: "/follow/record",
          name: "examplesRecord",
          meta: {
            title: "随访记录",
          },
          component: () => import("@/pages/examples/record/index.vue"),
        },
        {
          path: "/follow/main",
          name: "examplesMain",
          meta: {
            title: "新增随访",
          },
          component: () => import("@/pages/examples/main/index.vue"),
        },
        {
          path: "/settings/historical",
          name: "examplesHistorical",
          meta: {
            title: "历史随访记录",
          },
          component: () => import("@/pages/examples/historical/index.vue"),
        },
        {
          path: "/settings/doctor",
          name: "examplesDoctor",
          meta: {
            title: "医生管理",
          },
          component: () => import("@/pages/examples/doctor/index.vue"),
        },
        // {
        //   path: "obtainFollowUpStatus",
        //   name: "obtainFollowUpStatus",
        //   meta: {
        //     title: "患者管理",
        //   },
        //   component: () => import("@/pages/examples/obtainFollow/index.vue"),
        // },


      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/pages/examples/login/index.vue"),
    },
    {
      path: "/403",
      name: "403",
      component: () => import("@/pages/examples/errors/403.vue"),
    },
    // {
    //   // 404路由，以前是 * 号，现在换成 /:pathMatch(.*)*
    //   path: "/:pathMatch(.*)*",
    //   name: "404",
    //   component: () => import("@/pages/examples/errors/404.vue"),
    // },
  ],
});

// 全局前置守卫
router.beforeEach(async (to) => {
  if (to.name !== "login") {
    // 获取token
    const token = getToken();
    
    // 没有token，则直接打到登录页面
    if (!token||!sessionStorage.getItem("isLoggedIn")) {
      return {
        name: "login",
        query: {
          // redirect: to.fullPath,
        },
      };
    }

    // // 获取个人信息
    if (!store.state.user.person?.length) {
      
      const authRoutes = await store.dispatch("user/get");
      
      if(store.state.user.role=='普通用户'){
        // router.push('/follow/main')
        router.push('/physicals/health')
       store.commit("user/setCurTabName", "健康体检");
      // console.log('store.state.user.role',store.state.user.role);
      //  store.commit("user/setCurTabName", "慢病随访");
      }else{
        store.commit("user/setCurTabName", "首页");
        // router.push('/home')
      }

      // // 动态添加路由，使用 router.addRoute 方法
      // authRoutes.forEach((item) => {
      //   router.addRoute("index", {
      //     path: item.router,
      //     name: item.router.substr(1).split("/").join("_"),    
      //     // TODO. 上线后有问题，后续使用 import.meta.glob 一次导入多个模块的方式来解决
      //     // component: () => import(`/src/pages/${item.viewPath}`),
      //     component: () => import(`/src/pages/${item.router}/index.vue`),
      //   });
      // })
      // 再次重定向
    
    }
  }
});

export default router;
